package com.wedistudio.scribd.xml;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.wedistudio.android.framework.fetcher.URLBuilder;
import com.wedistudio.scribd.ScribdUtils;
import com.wedistudio.slidesprovider.Author;
import com.wedistudio.slidesprovider.Slide;
import com.wedistudio.slidesprovider.SlideID;
import com.wedistudio.slidesprovider.SlideTag;

@Root(name = "rsp")
public class RSP {

	@Attribute(required = false)
	public String stat;

	@Root(name = "error")
	public static class Error {
		@Attribute(required = false)
		public String code;

		@Attribute(required = false)
		public String message;
	}

	@Element(required = false)
	public Error error;

	@Root(name = "resultset")
	public static class ResultSet {

		@Attribute(required = false)
		public Integer totalResultsAvailable;

		@Attribute(required = false)
		public Integer totalResultsReturned;

		@Attribute(required = false)
		public Integer firstResultPosition;

		@Attribute(required = false)
		public Boolean list;

		@ElementList(required = false, inline = true, entry = "result")
		public List<Result> results;

		@Root(name = "result")
		public static class Result {
			@Element(required = false)
			public Integer doc_id;
			@Element(required = false)
			public String secret_password;
			@Element(required = false)
			public String access_key;
			@Element(required = false)
			public String title;
			@Element(required = false)
			public String description;
			@Element(required = false)
			public String tags;
			@Element(required = false)
			public String license;
			@Element(required = false)
			public String thumbnail_url;
			@Element(required = false)
			public Integer page_count;
			@Element(required = false)
			public String download_formats;
			@Element(required = false)
			public Float price;
			@Element(required = false)
			public Integer reads;
			@Element(required = false)
			public String uploaded_by;
			@Element(required = false)
			public String uploader_id;
			@Element(required = false)
			public String when_uploaded;
			@Element(required = false)
			public String when_updated;
			@Element(required = false)
			public Boolean available_on_api;

			public Slide getSlide() {
				try {
					Slide slide = Slide.create(SlideID.create(
							ScribdUtils.SOURCE, "" + doc_id));
					slide.setAuthor(Author.create(ScribdUtils.SOURCE,
							uploaded_by));
					slide.setSummary(description);
					slide.setTitle(title);
					if (thumbnail_url != null)
						slide.setThumbnail(URLBuilder.create(thumbnail_url));
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
					slide.setPublicationDate(when_updated == null ? sdf
							.parse(when_uploaded) : sdf.parse(when_updated));
					slide.setNbSlides(page_count);

					if (tags != null) {
						String Tags[] = tags.split(",");
						for (String tag : Tags) {
							slide.addTag(SlideTag.create(ScribdUtils.SOURCE,
									tag));
						}
					}
					return slide;
				} catch (Throwable e) {
					throw new RuntimeException(e);
				}
			}
		}
	}

	@Element(required = false, name = "result_set")
	public ResultSet resultset;

}
